﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using devtm.Collections;
using System.Xml.Linq;
using System.Reflection;

namespace devtm.Editor.TypeSelector
{


    public sealed class ObjectListe : INotifyPropertyChanged
    {


        private devtm.Collections.TableCollection<Type, string> _liste;
        devtm.Collections.ViewCollection<Type, string> _view;
        public ObjectListe()
        {
            _liste = new Collections.TableCollection<Type, string>(c => c.FullName);
            _view = _liste.GetView(c => IsValide(c));
            _item = new ObjectContainer();
        }

        private bool IsValide(Type c)
        {
            return true;
        }

        public ViewCollection<Type, string> Liste { get { return _view; } }

        private ObjectContainer _item;
        public ObjectContainer Item { get { return _item; } }

        private Type _selectedItem;

        public Type SelectedItem
        {
            get { return _selectedItem; }
            set
            {
                if (_selectedItem == value)
                    return;

                _selectedItem = value;
                MyPropertyChanged("SelectedItem");
                _item.Type = value;
                MyPropertyChanged("Item");

            }
        }



        protected void MyPropertyChanged(PropertyChangedEventArgs e)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, e);
        }

        protected void MyPropertyChanged(string propertyName)
        {
            MyPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;

        private const string spliter = "|";
        public override string ToString()
        {

            XElement root = new XElement("root", new XAttribute("name", SelectedItem.AssemblyQualifiedName));


            foreach (ValueParameter item in Item.ValuesParameters)
            {
                XElement v = item.ToAttribute();
                if (v != null)
                    root.Add(v);
            }

            return root.ToString(SaveOptions.DisableFormatting);
        }

        public void FromString(string attribute)
        {
            if (String.IsNullOrEmpty(attribute))
                return;

            XElement root = XElement.Parse(attribute);

            string i = root.Attribute("name").Value;
            if (!string.IsNullOrEmpty(i))
            {
                Type t = Type.GetType(i);
                if (t != null)
                {
                    SelectedItem = t;
                    foreach (XElement item in root.Elements())
                    {
                        i = item.Name.LocalName;
                        if (!string.IsNullOrEmpty(i))
                        {
                            if (this.Item.Parameters.ContainsKey(i))
                            {
                                Parameter j = this.Item.Parameters[i];
                                this.Item.ValuesParameters
                                    .Add(new ValueParameter(this.Item) { MyParameter = j, Value = item.Value });
                            }

                        }

                    }

                }
            }


            var oo = this.Item.ValuesParameters.Where(c => string.IsNullOrEmpty(c.Name)).Select(c => c.Key).ToList();
            foreach (var item in oo)
                this.Item.ValuesParameters.RemoveKey(item);
            
            if (this.Item.ValuesParameters.Count < this.Item.Parameters.Count)
                this.Item.ValuesParameters.AddParameter();

        }

    }

}
